/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AdvancedComicViewer.java
 *
 * Created on Jul 3, 2011, 2:25:44 PM
 */
package ACV;

import java.io.File;
import javax.swing.JFileChooser;

/**
 *
 * @author Calvin
 */
public class AdvancedComicViewer extends javax.swing.JFrame {
    protected ComicViewerEH comicViewerEH = null;

    /** Creates new form AdvancedComicViewer */
    public AdvancedComicViewer() {
        initComponents();
        comicViewerEH = new ComicViewerEH(this);
        comicViewerEH.setButtons(false);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        controlPanel = new javax.swing.JPanel();
        prevBtn = new javax.swing.JButton();
        scaleBtn = new javax.swing.JButton();
        nextBtn = new javax.swing.JButton();
        viewPanel = new javax.swing.JPanel();
        imagePanel = new ACV.ImagePanel();
        menuBar = new javax.swing.JMenuBar();
        fileMnu = new javax.swing.JMenu();
        openMnuItem = new javax.swing.JMenuItem();
        closeMnuItem = new javax.swing.JMenuItem();
        quitMnuItem = new javax.swing.JMenuItem();
        editMnu = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Advanced Comic Viewer");

        controlPanel.setPreferredSize(new java.awt.Dimension(400, 50));

        prevBtn.setText("Back");
        prevBtn.setPreferredSize(new java.awt.Dimension(65, 23));
        prevBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                prevBtnActionPerformed(evt);
            }
        });
        controlPanel.add(prevBtn);

        scaleBtn.setText("Scale");
        scaleBtn.setPreferredSize(new java.awt.Dimension(70, 23));
        scaleBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                scaleBtnActionPerformed(evt);
            }
        });
        controlPanel.add(scaleBtn);

        nextBtn.setText("Fwd");
        nextBtn.setPreferredSize(new java.awt.Dimension(65, 23));
        nextBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nextBtnActionPerformed(evt);
            }
        });
        controlPanel.add(nextBtn);

        getContentPane().add(controlPanel, java.awt.BorderLayout.SOUTH);

        viewPanel.setPreferredSize(new java.awt.Dimension(400, 500));
        viewPanel.setLayout(new java.awt.BorderLayout());

        imagePanel.setPreferredSize(new java.awt.Dimension(400, 500));
        imagePanel.setLayout(new java.awt.BorderLayout());
        viewPanel.add(imagePanel, java.awt.BorderLayout.CENTER);

        getContentPane().add(viewPanel, java.awt.BorderLayout.CENTER);

        fileMnu.setText("File");

        openMnuItem.setText("Open");
        openMnuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                openMnuItemActionPerformed(evt);
            }
        });
        fileMnu.add(openMnuItem);

        closeMnuItem.setText("Close");
        closeMnuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                closeMnuItemActionPerformed(evt);
            }
        });
        fileMnu.add(closeMnuItem);

        quitMnuItem.setText("Quit");
        quitMnuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                quitMnuItemActionPerformed(evt);
            }
        });
        fileMnu.add(quitMnuItem);

        menuBar.add(fileMnu);

        editMnu.setText("Edit");
        menuBar.add(editMnu);

        setJMenuBar(menuBar);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * This method displays the previous page in the current Comic.
     * Called when the user selects the Back button.
     * @param evt 
     */
    private void prevBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_prevBtnActionPerformed
        this.comicViewerEH.pageBack();
    }//GEN-LAST:event_prevBtnActionPerformed

    /**
     * This method scales the image of the current page to fit the screen.
     * Called when user selects the Scale button
     * @param evt 
     */
    private void scaleBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_scaleBtnActionPerformed
        this.comicViewerEH.scalePage();
    }//GEN-LAST:event_scaleBtnActionPerformed

    /**
     * This method disposes of the window and its components.
     * Called when the user selects quit from the menu.
     * @param evt 
     */
    private void quitMnuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_quitMnuItemActionPerformed
        this.setVisible(false);
        this.dispose();
    }//GEN-LAST:event_quitMnuItemActionPerformed
    
    /**
     * This method displays the next page in the current Comic
     * @param evt 
     */
    private void nextBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nextBtnActionPerformed
        this.comicViewerEH.pageForward();
    }//GEN-LAST:event_nextBtnActionPerformed

    /**
     * This method allows users to select the comic to open
     * @param evt 
     */
    private void openMnuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openMnuItemActionPerformed
        
        JFileChooser fileFinder = new JFileChooser();
	fileFinder.setCurrentDirectory(new File(System.getProperty("user.home")));
	fileFinder.setMultiSelectionEnabled(false);
	fileFinder.setFileSelectionMode(JFileChooser.FILES_ONLY);
	fileFinder.setFileFilter(new ComicFileFilter());
	int result = fileFinder.showOpenDialog(this);
	if(result == JFileChooser.APPROVE_OPTION) {

	    String path = fileFinder.getSelectedFile().getPath();  //file path
            this.comicViewerEH.openComic(path);
            //this.comicViewerEH.setButtons(true);
            //this.comicViewerEH.scalePage();
            fileFinder = null;

	}

    }//GEN-LAST:event_openMnuItemActionPerformed

    private void closeMnuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closeMnuItemActionPerformed
        this.comicViewerEH.closeComic();
    }//GEN-LAST:event_closeMnuItemActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new AdvancedComicViewer().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem closeMnuItem;
    public javax.swing.JPanel controlPanel;
    private javax.swing.JMenu editMnu;
    private javax.swing.JMenu fileMnu;
    public ACV.ImagePanel imagePanel;
    private javax.swing.JMenuBar menuBar;
    public javax.swing.JButton nextBtn;
    private javax.swing.JMenuItem openMnuItem;
    public javax.swing.JButton prevBtn;
    private javax.swing.JMenuItem quitMnuItem;
    public javax.swing.JButton scaleBtn;
    public javax.swing.JPanel viewPanel;
    // End of variables declaration//GEN-END:variables
}
